//
//  ProfileXMLWriter.m
//  MathMasterMain
//
//  Created by Jimmy Hahm on 10-03-29.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ProfileXMLWriter.h"


@implementation ProfileXMLWriter

-(void)startProfile //tries to load the PROFILE.xml file from Documents folder. if non-existant, it copies empty TEMPLATE PROFILET.XML
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"PROFILE.xml"];
	if([[NSString alloc] initWithContentsOfFile:appFile usedEncoding:nil error:nil] == nil)
	{
		NSString *filePath = [[NSBundle mainBundle] pathForResource:@"PROFILET" ofType:@"xml"];  
		NSData *myData = [NSData dataWithContentsOfFile:filePath];  
		if (myData) {  
			[myData writeToFile:appFile atomically:YES];
			}  
	}
		
		
}

-(void)writeProfile:(NSString *)name STUDENTGRADE:(NSString *)Grade //When called it adds name to current xml file
{
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"PROFILE.xml"];
	NSString *info = [[NSString alloc] initWithContentsOfFile:appFile];
	NSArray *arrayOfLines = [info componentsSeparatedByString:@"\n"];
	NSString *temp = [NSString new];
	
	BOOL EXISTS = NO;
	
	NSString* nameQ = [NSString stringWithFormat:@"%@%@%@",@"\t\t<Name>",name,@"</Name>"];
	
	for (NSString *M in arrayOfLines)
	{
		//UIAlertView *a = [[UIAlertView alloc] initWithTitle:nil message:M delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [a show]; [a release]; 
		
		
		if ([M isEqualToString:nameQ]) {
			EXISTS = YES;
		}
	
	}
	
	if (EXISTS) {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Same Name Exists on the Profile List, Please Choose a Different Name" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; 

	}else {
		int flag = 0;
		for (NSString *X in arrayOfLines)
		{
			
			if (flag!=1 && [X isEqualToString:@"\t\t<Name></Name>"]) 
			{
				X = [NSString stringWithFormat:@"%@%@%@", @"\t\t<Name>", name , @"</Name>"];
				flag = 1;
			}
			temp = [NSString stringWithFormat:@"%@%@%@", temp,X,@"\n"];
			
			
			
		}
		[temp writeToFile: appFile atomically: YES encoding:NSUTF8StringEncoding error:nil];		
	}

	
	
	
	
}

-(void)deleteProfile:(NSString*) profile{
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"PROFILE.xml"];
	NSString *info = [[NSString alloc] initWithContentsOfFile:appFile];
	NSArray *arrayOfLines = [info componentsSeparatedByString:@"\n"];
	NSString *temp = [NSString new];
	int flag = 0;
	NSString *q = [NSString stringWithFormat:@"%@%@%@", @"\t\t<Name>", profile , @"</Name>"];
	for (NSString *X in arrayOfLines)
	{
		if (flag!=1 && [X isEqualToString:q]) 
		{
			X = [NSString stringWithFormat:@"\t\t<Name></Name>"];
			flag = 1;
		}
		temp = [NSString stringWithFormat:@"%@%@%@", temp,X,@"\n"];
	}
	[temp writeToFile: appFile atomically: YES encoding:NSUTF8StringEncoding error:nil];
	
	
}
@end
